﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jungle.Rent;
using Jungle.Code.Core;
using Jungle.Code.Utils;


public partial class SearchBox : System.Web.UI.UserControl
{
    SessionKeeper sk = new SessionKeeper();
    protected override void OnInit(EventArgs e)
    {
        LoadStartData();
        LoadRequest();
        base.OnInit(e);
    }
    // ---------------------------------------------------------------------
    public void LoadStartData()
    {
        ddl_ObjectType.Items.Add(new ListItem("", "0"));
        foreach (ListItem l in Offer.ddl_ObjectType().Items) { ddl_ObjectType.Items.Add(l);}
        foreach (ListItem l in Offer.ddl_UnderType(1).Items) { if(l.Value != "1") ddl_Under1.Items.Add(l); }
        foreach (ListItem l in Offer.ddl_UnderType(2).Items) { ddl_Under2.Items.Add(l); }
        foreach (ListItem l in Offer.ddl_UnderType(3).Items) { ddl_Under3.Items.Add(l); }
        foreach (ListItem l in Offer.ddl_Region().Items) { ddl_Region.Items.Add(l); }
    }
    // ---------------------------------------------------------------------
    public void SendSearchRequest(object sender, EventArgs e)
    {
        HttpCookie cookie = Response.Cookies["SearchOptionsAlexConsult6"];
        cookie["op00"] = tb_OCode.Text.Trim();
        cookie["op01"] = "0";
        if (ddl_ObjectType.SelectedValue == "1") cookie["op01"] = ddl_Under1.SelectedValue;
        if (ddl_ObjectType.SelectedValue == "2") cookie["op01"] = ddl_Under2.SelectedValue;
        if (ddl_ObjectType.SelectedValue == "3") cookie["op01"] = ddl_Under3.SelectedValue;
        if (ddl_ObjectType.SelectedValue == "4") cookie["op01"] = "-4";
        if (ddl_ObjectType.SelectedValue == "5") cookie["op01"] = "-5";

        cookie["op02"] = ddl_Region.SelectedValue;
        cookie["op022"] = tb_City.Text.Trim();
        cookie["op03"] = ddl_SearchType.SelectedValue;

        if (tb_sq1.Text.Length == 0) cookie["op04"] = "0"; else cookie["op04"] = (Convert.ToDouble(tb_sq1.Text.Trim().Replace(".", ",")) * Convert.ToInt32(ddl_FielAreaType.SelectedValue)).ToString();
        if (tb_sq2.Text.Length == 0) cookie["op05"] = "1000000000"; else cookie["op05"] = (Convert.ToDouble(tb_sq2.Text.Trim().Replace(".", ",")) * Convert.ToInt32(ddl_FielAreaType.SelectedValue)).ToString();
        if (tb_sq3.Text.Length == 0) cookie["op06"] = "0"; else cookie["op06"] = (Convert.ToDouble(tb_sq3.Text.Trim().Replace(".", ",")) * Convert.ToInt32(ddl_FielAreaType.SelectedValue)).ToString();
        if (tb_sq4.Text.Length == 0) cookie["op07"] = "1000000000"; else cookie["op07"] = (Convert.ToDouble(tb_sq4.Text.Trim().Replace(".", ",")) * Convert.ToInt32(ddl_FielAreaType.SelectedValue)).ToString();

        if (tb_Price1.Text.Length == 0) cookie["op08"] = "0"; else cookie["op08"] = (Convert.ToDouble(tb_Price1.Text.Trim().Replace(".", ",")) * Convert.ToDouble(ddl_MoneyType.SelectedValue)).ToString();
        if (tb_Price2.Text.Length == 0) cookie["op09"] = "1000000000"; else cookie["op09"] = (Convert.ToDouble(tb_Price2.Text.Trim().Replace(".", ",")) * Convert.ToDouble(ddl_MoneyType.SelectedValue)).ToString();
        cookie["op10"] = ddl_MoneyType.SelectedValue;

        if (chb_Photo.Checked) cookie["op11"] = "true"; else cookie["op11"] = "false";
        if (chb_Komiss.Checked) cookie["op12"] = "true"; else cookie["op12"] = "false";
        if (chb_NewBuild.Checked) cookie["op13"] = "true"; else cookie["op13"] = "false";
        
        cookie.Expires = DateTime.Now.AddDays(14);

        Response.Redirect(UrlHelper.GetSiteUrl() + "/ru/search.aspx");
    }
    //------------------------------------------------------------------------------------------------------
    protected void LoadRequest()
    {
        HttpCookie cookie = Request.Cookies["SearchOptionsAlexConsult6"];
        if (cookie != null)
        {
            ddl_ObjectType.Items[0].Selected = false;

            if (Offer.ddl_UnderType(1).Items.FindByValue(cookie["op01"]) != null) { ddl_ObjectType.Items.FindByValue("1").Selected = true; ddl_Under1.Items.FindByValue(cookie["op01"]).Selected = true;}
            if (Offer.ddl_UnderType(2).Items.FindByValue(cookie["op01"]) != null) { ddl_ObjectType.Items.FindByValue("2").Selected = true; ddl_Under2.Items.FindByValue(cookie["op01"]).Selected = true;}
            if (Offer.ddl_UnderType(3).Items.FindByValue(cookie["op01"]) != null) { ddl_ObjectType.Items.FindByValue("3").Selected = true; ddl_Under3.Items.FindByValue(cookie["op01"]).Selected = true; }
            if (cookie["op01"] == "-1")  ddl_ObjectType.Items.FindByValue("1").Selected = true;
            if (cookie["op01"] == "-2") ddl_ObjectType.Items.FindByValue("2").Selected = true;
            if (cookie["op01"] == "-3") ddl_ObjectType.Items.FindByValue("3").Selected = true;
            if (cookie["op01"] == "-4") ddl_ObjectType.Items.FindByValue("4").Selected = true;
            if (cookie["op01"] == "-5") ddl_ObjectType.Items.FindByValue("5").Selected = true;
            if (cookie["op01"].Length > 2)
            {
                ddl_ObjectType.Items.FindByValue("2").Selected = true;
                ddl_Under2.Items.FindByText(cookie["op01"]).Selected = true;
            }


            ddl_Region.Items[0].Selected = false;
            ddl_Region.Items.FindByValue(cookie["op02"]).Selected = true;

            tb_City.Text = cookie["op022"];

            ddl_SearchType.Items.FindByValue(cookie["op03"]).Selected = true;
            tb_sq1.Text = cookie["op04"]; tb_sq2.Text = cookie["op05"];
            tb_sq3.Text = cookie["op06"]; tb_sq4.Text = cookie["op07"];

            if (cookie["op05"] == "1000000000") tb_sq2.Text = "";
            if (cookie["op04"] == "0") tb_sq1.Text = "";
            if (cookie["op07"] == "1000000000") tb_sq4.Text = "";
            if (cookie["op06"] == "0") tb_sq3.Text = "";

            
            tb_Price1.Text = cookie["op08"]; tb_Price2.Text = cookie["op09"];
            if (cookie["op09"] == "1000000000") tb_Price2.Text = "";
            if (cookie["op08"] == "0") tb_Price1.Text = "";
            //ddl_MoneyType.Items.FindByValue(cookie["op10"]).Selected = true;
            chb_Photo.Checked = Convert.ToBoolean(cookie["op11"]);
            chb_Komiss.Checked = Convert.ToBoolean(cookie["op12"]);
            chb_NewBuild.Checked = Convert.ToBoolean(cookie["op13"]);
        }
    }
}
